//
//  WHHomeViewController.swift
//  8.31-WH微博
//
//  Created by 吴涵 on 15/8/31.
//  Copyright © 2015年 wuhan. All rights reserved.
//

import UIKit
import SVProgressHUD


class WHHomeViewController: BaseTableViewController {

   override func viewDidLoad() {
    super.viewDidLoad()
      if !UserAccountViewModel.sharedUserAccount.userLogin{
          view1?.setUpInfo(nil, label: "关注一些人，回这里看看有什么惊喜")
          return
    }
      loadData()
       tableView.registerClass(WHForwordCell.self, forCellReuseIdentifier: WHForwardCellID)
    tableView.registerClass(WHNormalCell.self, forCellReuseIdentifier: WHNormalCellID)
       tableView.estimatedRowHeight = 200
       tableView.rowHeight = 300
    //UITableViewAutomaticDimension
    tableView.separatorStyle = UITableViewCellSeparatorStyle.None
    
   
    }
  
    
    private func loadData(){
        statusViewModel.loadStatus().subscribeError({ (err) -> Void in
            printLog(err)
            SVProgressHUD.showInfoWithStatus("home加载微博数据中调用，网络不给力")
            }) { () -> Void in
                self.tableView.reloadData()
        }
    
    }

    private lazy var statusViewModel: WHStatusViewModel =  WHStatusViewModel()

}
extension WHHomeViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusViewModel.statuses.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let viewModel = statusViewModel.statuses[indexPath.row] as! WHUserStatusModel
        
        let cell = tableView.dequeueReusableCellWithIdentifier(viewModel.cellID, forIndexPath: indexPath) as! WHStatusViewCell
        cell.statusViewModel = viewModel
        
        return cell
    }
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        let viewModel = statusViewModel.statuses[indexPath.row] as! WHUserStatusModel
        
        let cell = tableView.dequeueReusableCellWithIdentifier(viewModel.cellID) as! WHStatusViewCell
        print(cell.rowHeight(viewModel))
        return cell.rowHeight(viewModel)
    }

}